import {request} from "./request.js"
var COS = require("./cos-wx-sdk-v5.js")
const baseUrl = "http://localhost:8700"


export const wxLoginApi = () =>{
	console.log("start api-wxLogin")
	return new Promise((resolve, reject) => {
		wx.login({
			provider: 'weixin',
			success(res) {
				//{errMsg: "login:ok", code: "0c1gZp0w3rIRJ23W920w3PTH4p0gZp0L"}
				console.log(res)
				resolve(res.code)
			},
			fail(res){
				reject(res.errMsg)
			}
		})
	}).then(data => {
		var opt = {
			url: baseUrl + "/auth/wx/login",
			method: "post",
			data: data,
		}
		return request(opt);
	})
}

export const getUserInfoApi = ()=>{
	var opt = {};
	opt.url = baseUrl + "/user/get/userInfo";
	opt.method = "get";
	return request(opt)
}

export const analyseSingleApi = (analyseUrl) => {
  var data = {
    url: analyseUrl
  };
  console.log("aa")
  console.log(data)
  var opt = {
    url : baseUrl + "/media/analyse/single/analyse",
    method: "post",
    data: data,
  }
  return request(opt)
}
export const getAnalyseRecords = () => {
  var opt = {
    url : baseUrl + "/user/get/analyseRecords",
    method: "get",
  }
  return request(opt)
}

export const saveUserInfoApi = (data) => {
  var opt = {
    url: baseUrl + "/user/save",
    method: "post",
    data: data,
  }
  return request(opt)
}

export const getOSSCredential = () => {
  console.log("getOssCred")
  var data = {
    ossPlatform: "tencent",
    bucket: "dr-1259228989",
    prefixes: "avatar/*",
  }
  var opt = {
    url: baseUrl + "/auth/get/oss/credential",
    method: "post",
    data: data,
  }
  return request(opt)
}
export const getCos = () => {
  console.log("get Cos")
  return new COS({
    SimpleUploadMethod: 'putObject',
    getAuthorization: function (options, callback) {
      getOSSCredential().then( res => {
        var data = res.data;
        var credentials = data && data.credentials;
        console.log("credentials : ",credentials)
        if (!data || !credentials) return console.error('credentials invalid');
        callback({
            TmpSecretId: credentials.tmpSecretId,
            TmpSecretKey: credentials.tmpSecretKey,
            // v1.2.0之前版本的 SDK 使用 XCosSecurityToken 而不是 SecurityToken
            SecurityToken: credentials.sessionToken,
            // 建议返回服务器时间作为签名的开始时间，避免用户浏览器本地时间偏差过大导致签名错误
            StartTime: data.startTime, // 时间戳，单位秒，如：1580000000
            ExpiredTime: data.expiredTime, // 时间戳，单位秒，如：1580000900
        });
      })
    }

  })
}

export const deleteAnalyseRecord = (id)=>{
	var opt = {};
	opt.url = baseUrl + "/user/del/analyseRecords/" + id;
	opt.method = "post";
	return request(opt)
}
